package com.flower.enjoyment.flower.controller;

import com.alibaba.fastjson.JSONObject;
import com.flower.enjoyment.flower.base.PageBean;
import com.flower.enjoyment.flower.base.ResultMsg;
import com.flower.enjoyment.flower.entity.UserCashOut;
import com.flower.enjoyment.flower.entityDo.*;
import com.flower.enjoyment.flower.enumUtils.ResultEnum;
import com.flower.enjoyment.flower.service.*;
import com.flower.enjoyment.flower.utils.RandomUtil;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/userApi")
@Api(value = "用户信息", description = "用户信息")
public class UserController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserYuYueService userYuYueService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserAssessService userAssessService;
    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private FlowersCardOrderService flowersCardOrderService;

    @Autowired
    private FlowersCardOrderDetailService flowersCardOrderDetailService;
    @Autowired
    private PresentDetailService presentDetailService;
    @Autowired
    private UserCashOutService userCashOutService;

    @ApiOperation(value = "用户个人信息", notes = "用户信息")
    @ResponseBody
    @GetMapping("/userInfo")
    public ResultMsg getUserInfo(
            @ApiParam(value = "userId") @RequestParam("userId") String userId) {
        UserInfoDo userInfoDo = userInfoService.selectById(userId);
        if (userInfoDo==null){
            return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), new UserInfoDo());
        }
        userInfoDo.setYuyueCou(0);
        if (userInfoDo.getIsBoss()==1){
            userInfoDo.setYuyueCou(userYuYueService.selectCountByShopId(userId));
            double todayAmount = orderInfoService.selectSumAmountByToday(userId);
            if (todayAmount != userInfoDo.getTodayIncome()){
                logger.info("用户：{}，今日收入金额和用户表中不一致，请注意",userInfoDo.getUserName());
                userInfoDo.setTodayIncome(todayAmount);
            }
            double totalAmount = orderInfoService.selectSumAmountByAll(userId);
            if (totalAmount != userInfoDo.getTotalIncome()) {
                logger.info("用户：{}，全部收入金额和用户表中不一致，请注意", userInfoDo.getUserName());
                userInfoDo.setTotalIncome(totalAmount);
            }
        }
        ShopInfoDo shopInfoDo = shopService.selectByUserId(userId);
        if (shopInfoDo != null)
            userInfoDo.setShopId(shopInfoDo.getId());
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), userInfoDo);

    }
    @ApiOperation(value = "我的花享卡列表", notes = "花享卡列表")
    @ResponseBody
    @GetMapping("/getFlowerCardListByUserId")
    public ResultMsg<FlowerCardDo> getFlowerCardListByUserId(
            @ApiParam(value = "userId") @RequestParam("userId") String userId) {
        List<FlowerCardDo> flowerCardOrderList = flowersCardOrderService.selectByUserId(userId);

        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), flowerCardOrderList);

    }

    @ApiOperation(value = "使用明细", notes = "使用明细")
    @ResponseBody
    @GetMapping("/flowerCardUseList")
    public ResultMsg<FlowerCardOrderDetailDo> getFlowerCardUseList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                                   @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        PageBean<FlowerCardOrderDetailDo> flowerCardOrderDetailDoPageBean = new PageBean<FlowerCardOrderDetailDo>();
        try {
            Page<FlowerCardOrderDetailDo> flowerCardOrderDetailDoPage = flowersCardOrderDetailService.getFlowerCardOrderListByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(flowerCardOrderDetailDoPageBean, flowerCardOrderDetailDoPage);

            flowerCardOrderDetailDoPageBean.setItems(flowerCardOrderDetailDoPage.getResult());
        } catch (Exception e) {
            logger.error("查询使用明细记录失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), flowerCardOrderDetailDoPageBean);


    }

    @ApiOperation(value = "使用明细", notes = "使用明细")
    @ResponseBody
    @GetMapping("/getPresentDetailList")
    public ResultMsg<PresentDetailDo> getPresentDetailList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        PageBean<PresentDetailDo> presentDetailDoPageBean = new PageBean<PresentDetailDo>();
        try {
            Page<PresentDetailDo> presentDetailDoPage = presentDetailService.selectPresentDetailByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(presentDetailDoPageBean, presentDetailDoPage);

            presentDetailDoPageBean.setItems(presentDetailDoPage.getResult());
        } catch (Exception e) {
            logger.error("查询赠送金额使用明细记录失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), presentDetailDoPageBean);
    }

    @ApiOperation(value = "消费记录", notes = "消费记录")
    @ResponseBody
    @GetMapping("/getConsumeDetailList")
    public ResultMsg<OrderInfoDo> getConsumeDetailList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                       @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        PageBean<OrderInfoDo> orderInfoDoPageBean = new PageBean<OrderInfoDo>();
        try {
            Page<OrderInfoDo> orderInfoDoPage = orderInfoService.getSuccessOrderListByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(orderInfoDoPageBean, orderInfoDoPage);

            orderInfoDoPageBean.setItems(orderInfoDoPage.getResult());
        } catch (Exception e) {
            logger.error("查询我的消费记录失败：{}", e.getMessage());
        }
        logger.error("查询我的消费记录返回信息：{}", JSONObject.toJSONString(orderInfoDoPageBean));
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), orderInfoDoPageBean);


    }

    @ApiOperation(value = "我的预约", notes = "我的预约")
    @ResponseBody
    @GetMapping("/orderList")
    public ResultMsg<UserAppointmentDo> getOrderList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {

        PageBean<UserAppointmentDo> userYuYueDoPageBean = new PageBean<UserAppointmentDo>();
        try {
            Page<UserAppointmentDo> userYuYueDos = userYuYueService.selectUserYuYueListByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(userYuYueDoPageBean, userYuYueDos);

            userYuYueDoPageBean.setItems(userYuYueDos.getResult());
        } catch (Exception e) {
            logger.error("查询我的预约列表失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), userYuYueDoPageBean);

    }

    @ApiOperation(value = "常去店铺", notes = "常去店铺")
    @ResponseBody
    @GetMapping("/selectResortShopList")
    public ResultMsg<ShopInfoDo> selectResortShopList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                      @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {

        PageBean<ShopInfoDo> shopInfoDoPageBean = new PageBean<ShopInfoDo>();
        try {
            Page<ShopInfoDo> shopInfoDoPage = shopService.selectResortShopListByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(shopInfoDoPageBean, shopInfoDoPage);
            shopInfoDoPageBean.setItems(shopInfoDoPage.getResult());
        } catch (Exception e) {
            logger.error("获取常去店铺列表失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), shopInfoDoPageBean);

    }

    @ApiOperation(value = "我的评价", notes = "我的评价")
    @ResponseBody
    @GetMapping("/commentList")
    public ResultMsg<UserAssessDo> getCommentList(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        PageBean<UserAssessDo> userAssessDoPageBean = new PageBean<UserAssessDo>();
        try {
            Page<UserAssessDo> userAssessPage = userAssessService.selectUserAssessListUserIdByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(userAssessDoPageBean, userAssessPage);
            userAssessDoPageBean.setItems(userAssessPage.getResult());
        } catch (Exception e) {
            logger.error("查询门店列表失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), userAssessDoPageBean);

    }

    @ApiOperation(value = "我的商铺", notes = "我的店铺")
    @ResponseBody
    @GetMapping("/getShopInfoByUserId")
    public ResultMsg<ShopInfoDo> getShopInfoByUserId(
            @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        logger.info("我的商铺：{}", userId);
        ShopInfoDo shopInfoDo = shopService.selectByUserId(userId);
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), shopInfoDo);

    }

    @ApiOperation(value = "提现列表", notes = "提现列表")
    @ResponseBody
    @GetMapping("/getUserCashOutListByUserId")
    public ResultMsg<UserCashOutDo> getUserCashOutListByUserId(@ApiParam(value = "当前页") @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @ApiParam(value = "每页条数") @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                               @ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        PageBean<UserCashOutDo> userCashOutDoPageBean = new PageBean<UserCashOutDo>();
        try {
            Page<UserCashOutDo> userCashOutDoPage = userCashOutService.selectUserCashOutByPage(pageNo, pageSize, userId);

            BeanUtils.copyProperties(userCashOutDoPageBean, userCashOutDoPage);

            userCashOutDoPageBean.setItems(userCashOutDoPage.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("查询用户提现列表失败：{}", e.getMessage());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), userCashOutDoPageBean);
    }

    @ApiOperation(value = "保存提现信息", notes = "保存提现信息")
    @ResponseBody
    @GetMapping("/saveUserCashOutInfo")
    public ResultMsg saveUserCashOutInfo(@ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId,
                                         @ApiParam(value = "提现金额") @RequestParam(value = "amount") double amount,
                                         @ApiParam(value = "微信号") @RequestParam(value = "wechatNum") String wechatNum) {

        try {
            UserCashOut userCashOut = new UserCashOut();
            userCashOut.setId(RandomUtil.getUuid());
            userCashOut.setCreateTime(new Date());
            userCashOut.setCashAmount(amount);
            userCashOut.setUserId(userId);
            userCashOut.setWechatId(wechatNum);
            userCashOut.setStatus(0);
            userCashOutService.insertUserCashOut(userCashOut);
            return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), null);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("保存用户提现申请失败：", e);
            return new ResultMsg(ResultEnum.ERROR_CODE.getCode());
        }

    }

    @ApiOperation(value = "获取通过我分享进来的用户", notes = "获取通过我分享进来的用户")
    @ResponseBody
    @GetMapping("/getUserInfoBySourceUserId")
    public ResultMsg<UserInfoDo> getUserInfoBySourceUserId(@ApiParam(value = "userId") @RequestParam(value = "userId", required = true) String userId) {
        List<UserInfoDo> userInfoDoList = null;
        try{
            userInfoDoList = userInfoService.selectListBySourceUserId(userId);
        } catch (Exception e) {
            logger.error("获取通过我分享进来的数据失败：{}", e.getMessage());
            return new ResultMsg(ResultEnum.ERROR_CODE.getCode());
        }
        return new ResultMsg(ResultEnum.SUCCESS_CODE.getCode(), userInfoDoList);


    }
}
